<?php

namespace App\Controller;

use App\Service\ElasticSearch;
use Hyperf\HttpServer\Annotation\AutoController;


/**
 * @AutoController()
 */
class EsController extends BaseController
{
    public function index()
    {
        try {
            $es = new ElasticSearch();
            return $es->indexEs([
                'index'=>'higo_log',
                'body'=>json_decode("{\"time\":\"2023-02-10T13:43:04+08:00\",\"type\":\"nginx\",\"msg\":\"毫米啊\",\"time_stamp\":1676007784}"),
                'type'=>'nginx'
            ]);
        }catch (\Throwable $throwable){
            var_dump($throwable->getMessage());
        }
    }

    public function search()
    {
        try {
            $es = new ElasticSearch();
            $searchResult = $es->search([
                'index'=>'higo_log'
            ]);
            return $searchResult['hits']['hits'] ?? [];
        }catch (\Throwable $throwable){
            return $throwable->getMessage();
        }
    }

    public function count()
    {
        try {
            $es = new ElasticSearch();
            return $es->count([
                'index'=>'higo_log'
            ]);
        }catch (\Throwable $throwable){
            return $throwable->getMessage();
        }
    }

    public function del()
    {
        try {
            $es = new ElasticSearch();
            return $es->deleteIndex('higo_api_log');
        }catch (\Throwable $throwable){
            return $throwable->getMessage();
        }
    }
}